package com.javabasic.algorithm.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/11/3 12:44 上午
 * @see [167. Two Sum II - Input Array Is Sorted](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/)
 */
public class TwoSumIIInputArrayIsSorted {

    /**
     * 利用MAP暴力解法
     * @param numbers
     * @param target
     * @return
     */
    public int[] twoSum(int[] numbers, int target) {
        Map<Integer, List<Integer>> numTag = new HashMap<>();
        int len = numbers.length;
        List<Integer> orDefault;
        for (int i = 0; i < len; i++) {
            orDefault = numTag.getOrDefault(numbers[i], new ArrayList<>());
            orDefault.add(i+1);
            numTag.put(numbers[i],orDefault);
        }
        Set<Integer> keys = numTag.keySet();
        int[] result = new int[]{-1,-1};
        for (Integer key : keys) {
            if ((key*2 != target)) {
                if (numTag.containsKey(target-key)) {
                    System.out.println(key + " -- " + (target - key));
                    result[0] = numTag.getOrDefault(key,new ArrayList<>()).get(0);
                    result[1] = numTag.getOrDefault(target-key,new ArrayList<>()).get(0);
                    Arrays.sort(result);
                    return result;
                }
            } else {
                orDefault = numTag.getOrDefault(key, new ArrayList<>());
                if (orDefault.size() > 1) {
                    System.out.println(key + " -- " + (target - key));
                    result[0] = numTag.getOrDefault(key,new ArrayList<>()).get(0);
                    result[1] = numTag.getOrDefault(key,new ArrayList<>()).get(1);
                    Arrays.sort(result);
                    return result;
                }
            }
        }
        return result;
    }
}
